package image_list

import (
	link "bridge/link_docker"
	"context"
	"encoding/json"
	"fmt"
	"github.com/docker/docker/api/types"
	"net/http"
)

func Image_list(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	fmt.Println(r.Header)
	cli := link.Client()
	images, err := cli.ImageList(context.Background(), types.ImageListOptions{})
	if err != nil {
		panic(err)
	}
	type Image struct {
		Name string `json:"name"`
	}
	type Imageslice struct {
		Images []Image `json:"images"`
	}
	var Imagec Imageslice
	for _, image := range images {
		fmt.Println(image)
		Imagec.Images = append(Imagec.Images, Image{Name: image.RepoTags[0]})
	}
	b, err := json.Marshal(Imagec)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(b))
	fmt.Fprint(w, string(b))
}
